package com.mesd.killer.job;

import java.io.File;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import com.mesd.framework.util.MailSenderInfo;
import com.mesd.framework.util.SimpleMailSender;
import com.mesd.killer.filter.SearchBean;
import com.mesd.killer.importer.CupGameResultManager;
import com.mesd.killer.importer.GameDetailReader;
import com.mesd.killer.importer.GameResultManager;
import com.mesd.killer.model.GameResult;
import com.mesd.killer.service.KillerService;

@Service("killerJob")
public class KillerJob
{

	GameDetailReader reader = new GameDetailReader();

	GameResultManager manager = new GameResultManager();

	CupGameResultManager cupManager = new CupGameResultManager();

	Logger logger = Logger.getLogger(this.getClass());

	@Autowired
	private KillerService killerService;

	/**
	 * ÿ0��10��20��30��40��50��ˢ��
	 */
	@Scheduled(cron = "0/10 * * * * ?")
	public void workInTenSeconds()
	{
		// logger.debug("this is a job");
	}

	/**
	 * ÿ5��10��15��20��25��30��35��40��45��50��55����ˢ��
	 */
	@Scheduled(cron = "0 2/5 * * * ?")
	public void workInFiveMinutes()
	{
		System.out.println("Start Update");
		reader.saveDetailData();
		System.out.println("Update Success");
		List<GameResult> gameResults = killerService.searchCurrentGameResult();
		List<GameResult> daqiuGameResults = killerService.searchDaqiuGameResult();
		for (GameResult gameResult : gameResults)
		{
			if(!daqiuGameResults.contains(gameResult))
			{
				continue;
			}
			long gameId = gameResult.getGameId();
			SearchBean bean = new SearchBean();
			bean.setHasDaxiao(0);
			bean.setShuiweiOffset((float) 0.1);
			bean.setBigOffset((float) 0.2);
			bean.setGameId(gameId);
			List<GameResult> analysisResult = killerService.SearchGameResult(bean);
			String betTime = killerService.getAnalysisResult(
				analysisResult, true);
			if (!betTime.equals(""))
			{
				String reminder =gameResult.getGameTime().toLocaleString() + " "
					+ gameResult.getLeagueName() + " " + gameResult.getHostTeam() + "-"
					+ gameResult.getVisitTeam() + " 根据水位偏离值0.1的查询结果：" + betTime;
				// 推送提示消息
				sendMail(reminder);
				System.out.println("Send mail success!");
			}
			SearchBean bean2 = new SearchBean();
			bean2.setHasDaxiao(1);
			bean2.setShuiweiOffset((float) 0.2);
			bean2.setBigOffset((float) 0.2);
			bean2.setGameId(gameId);
			List<GameResult> analysisResult2 = killerService.SearchGameResult(bean2);
			String betTime2 = killerService.getAnalysisResult(
				analysisResult2, true);
			if (!betTime2.equals(""))
			{
				String reminder = gameResult.getGameTime().toLocaleString() + " "
					+ gameResult.getLeagueName() + " " + gameResult.getHostTeam() + "-"
					+ gameResult.getVisitTeam() + " 根据水位偏离值0.2和大小偏离值0.2的查询结果：" + betTime2;
				// 推送提示消息
				sendMail(reminder);
				System.out.println("Send mail success!");
			}
		}
	}

	/**
	 * ÿ��12��ˢ��һ��
	 */
	@Scheduled(cron = "0 0 12 * * ?")
	public void workInHours()
	{
		System.out.println("Start import NewLeagueSaichen");
		manager.importNewLeagueSaichen();
		cupManager.importNewLeagueSaichen();
		System.out.println("End import NewLeagueSaichen");
//		List<GameResult> daqiuGameResults = killerService.searchDaqiuGameResult();
//		StringBuffer sb = new StringBuffer();
//		sb.append("今日潜在大球赛事有："+"\n");
//		for(GameResult gameResult:daqiuGameResults)
//		{
//			String reminder = gameResult.getGameTime().toLocaleString() + " "
//				+ gameResult.getLeagueName() + " " + gameResult.getHostTeam() + "-"
//				+ gameResult.getVisitTeam();
//			// 推送提示消息
//			sb.append(reminder+"\n");
//		}
//		sendMailToMe(sb.toString());
	}

	public void sendMail(String content)
	{
		MailSenderInfo mailInfo = new MailSenderInfo();
		mailInfo.setMailServerHost("smtp.yeah.net");
		mailInfo.setMailServerPort("25");
		mailInfo.setValidate(true);
		mailInfo.setUserName("killer_mesd@yeah.net");
		mailInfo.setPassword("killer036100");// 您的邮箱密码
		mailInfo.setFromAddress("killer_mesd@yeah.net");
		mailInfo.setToAddress(new String[] { "paul_deng@yeah.net", "putianchen44@163.com",
			"sales_mes@yeah.net","185668645@qq.com" });
		mailInfo.setSubject("凯勒分析结果提示消息");
		mailInfo.setContent(content);
		// 这个类主要来发送邮件
		SimpleMailSender sms = new SimpleMailSender();
		sms.sendHtmlMail(mailInfo);// 发送html格式
	}

	public void sendMailToMe(String content)
	{
		MailSenderInfo mailInfo = new MailSenderInfo();
		mailInfo.setMailServerHost("smtp.yeah.net");
		mailInfo.setMailServerPort("25");
		mailInfo.setValidate(true);
		mailInfo.setUserName("killer_mesd@yeah.net");
		mailInfo.setPassword("killer036100");// 您的邮箱密码
		mailInfo.setFromAddress("killer_mesd@yeah.net");
		mailInfo.setToAddress(new String[] { "paul_deng@yeah.net"});
		mailInfo.setSubject("凯勒分析结果提示");
		
		mailInfo.setContent(content);
		// 这个类主要来发送邮件
		SimpleMailSender sms = new SimpleMailSender();
		sms.sendHtmlMail(mailInfo);// 发送html格式
	}
	
	public static void main(String[] args)
	{
		KillerJob job = new KillerJob();
		job.workInFiveMinutes();
	}
}
